/**
 * Javascript file for sources needed at change_password.php
 */

function check_delete_account(field) {
	if (validate_form() == false) return false;
	
	if (field == true) {
		return confirm(delete_account_safety_request);
	}
	return true;
}

function validate_form() {
	var fields = new Array("oldpass","stylepath","email");
	var i, error;

	// 1. validate that all fields are valid
	for (i=0; i<fields.length; i++) {
		if (dijit.byId(fields[i]).get("state") != "") {
			dijit.byId(fields[i]).set("state","Error");
			return false;
		}
	}
	return true;
}

function update_links() {
	// hide update button to avoid further clicks
	dojo.byId("links_buttons").style.display = "none";
	dojo.byId("messageArea").style.display  = "none";
	var xhrArgs = {
        url: "ajax/ajax_change_password.php?type=updatelinks",
        form: dojo.byId("update_links"),
        handleAs: "json",
        load: function(jsonData) {
        	showMessages(jsonData);
       	
        	// reload content
        	dijit.byId("tab2").set("href", "ajax/ajax_options_links.php");
        },
        error: function(error) {
			console.log("Error:");
			console.log(error);
        }
    }	
	dojo.xhrPost(xhrArgs);
	
}

function update_bbcodes() {
	// hide update button to avoid further clicks
	dojo.byId("bbcode_buttons").style.display = "none";
	dojo.byId("messageArea").style.display  = "none";
	var xhrArgs = {
        url: "ajax/ajax_change_password.php?type=updatebbcodes",
        form: dojo.byId("update_bbcodes"),
        handleAs: "json",
        load: function(jsonData) {
        	showMessages(jsonData);
        	
        	// reload content
        	dijit.byId("tab3").set("href", "ajax/ajax_options_bbcodes.php");
        },
        error: function(error) {
			console.log("Error:");
			console.log(error);
        }
    }	
	dojo.xhrPost(xhrArgs);
}

function perform_useradmin() {
	// hide update button to avoid further clicks
	dojo.byId("admin_button").style.display = "none";
	dojo.byId("messageArea").style.display  = "none";
	var xhrArgs = {
        url: "ajax/ajax_change_password.php?type=perform_useradmin",
        form: dojo.byId("usermangagement"),
        handleAs: "json",
        load: function(jsonData) {
        	showMessages(jsonData);
        	
        	// reload content
        	dijit.byId("tab5").set("href", "ajax/ajax_options_own_management.php");
        },
        error: function(error) {
			console.log("Error:");
			console.log(error);
        }
    }	
	dojo.xhrPost(xhrArgs);
}

function adjust_color(control) {
	var name = control.get("name")+"_e";
	try {
		dojo.byId(name).style.color = control.value;
	} catch(ignore) {
		return;
	}
}

function galaxytool_change_password_init() {
	// select user defined tab
	var choosen_tab = window.location.hash;
	if (choosen_tab != "") {
		try {
			choosen_tab = choosen_tab.replace(/#select_/, "");
			dijit.byId("tabcontainer").selectChild(dijit.byId(choosen_tab));
		} catch (ignore) {
			// select default tab in case of wrong user input
		}
	}

	dojo.subscribe("tabcontainer-selectChild", function(child) {
		window.location.hash = "#select_" + child.get("id");
		// disable message area
		dojo.byId("messageArea").style.display  = "none";
	});
}

dojo.ready(galaxytool_change_password_init);
